package com.af.monitorModule.generator.mapper;

import com.af.monitorModule.generator.entity.Fragment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-11-15
 */
@Component
public interface FragmentMapper extends BaseMapper<Fragment> {

    @Select("select company_id as company from monitor where id = #{monitor}")
    List<Map<String,Integer>> getCompanyId(@Param("monitor") Integer monitor);

    @Select("select b.company_name,a.* from (\n" +
            "SELECT DISTINCT company_id,db_name \n" +
            "FROM fragment \n" +
            "GROUP BY company_id,db_name) a left join company b on a.company_id = b.id " +
            "ORDER BY b.company_name")
    List<Map<String,Object>> getFragmentGroup();


    @Select("SELECT * \n" +
            "FROM fragment \n" +
            "WHERE company_id = #{company} AND db_name = #{dbName} \n" +
            "ORDER BY input_date DESC \n" +
            "LIMIT 1")
    Fragment findNewOne(@Param("company") Integer company, @Param("dbName") String dbName);
}
